<!DOCTYPE html>
<html>
<head><meta charset="utf-8" />
<title>301-函数</title>

<script src="/lib/require/index.js"></script>
<script src="/lib/jquery/index.js"></script>

<!-- 改为引入外部样式 -->
<link rel="stylesheet" href="/css/custom.css">

<!-- Loading mathjax macro -->
<!-- Load mathjax -->
    <script src="/lib/mathjax/index.js"></script>
    <!-- MathJax configuration -->
    <script type="text/x-mathjax-config">
    MathJax.Hub.Config({
        tex2jax: {
            inlineMath: [ ['$','$'], ["\\(","\\)"] ],
            displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
            processEscapes: true,
            processEnvironments: true
        },
        // Center justify equations in code and markdown cells. Elsewhere
        // we use CSS to left justify single line equations in code cells.
        displayAlign: 'center',
        "HTML-CSS": {
            styles: {'.MathJax_Display': {"margin": 0}},
            linebreaks: { automatic: true }
        }
    });
    </script>
    <!-- End of mathjax configuration --></head>
<body>
<div class="toc">
<ul>
                        
                        
                        <li class="toc-li-2">
                            <a href="#函数">函数</a>
                        </li>
                    
                
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#return语句">return语句</a>
                        </li>
                    
                
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#函数的属性">函数的属性</a>
                        </li>
                    
                
            
            
                        
                        
                        <li class="toc-li-4">
                            <a href="#`__doc__`">`__doc__`</a>
                        </li>
                    
                
            
            
            
                        
                        
                        <li class="toc-li-4">
                            <a href="#增加函数属性">增加函数属性</a>
                        </li>
                    
                
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#参数">参数</a>
                        </li>
                    
                
            
            
            
            
            
            
            
            
            
            
                        
                        
                        <li class="toc-li-4">
                            <a href="#必需参数">必需参数</a>
                        </li>
                    
                
            
            
                        
                        
                        <li class="toc-li-4">
                            <a href="#关键字参数">关键字参数</a>
                        </li>
                    
                
            
            
            
                        
                        
                        <li class="toc-li-4">
                            <a href="#默认参数">默认参数</a>
                        </li>
                    
                
            
            
            
                        
                        
                        <li class="toc-li-4">
                            <a href="#不定长参数">不定长参数</a>
                        </li>
                    
                
            
            
                        
                        
                        <li class="toc-li-5">
                            <a href="#*args">*args</a>
                        </li>
                    
                
            
            
            
            
            
                        
                        
                        <li class="toc-li-5">
                            <a href="#**kargs">**kargs</a>
                        </li>
                    
                
            
            
            
            
            
            
                        
                        
                        <li class="toc-li-4">
                            <a href="#多种类型参数">多种类型参数</a>
                        </li>
                    
                
            
                        
                        
                        <li class="toc-li-4">
                            <a href="#解包实参调用函数">解包实参调用函数</a>
                        </li>
                    
                
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#递归">递归</a>
                        </li>
                    
                
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
                        
                        
                        <li class="toc-li-2">
                            <a href="#函数式编程">函数式编程</a>
                        </li>
                    
                
            
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#匿名函数lambda">匿名函数lambda</a>
                        </li>
                    
                
            
            
            
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#map(function,-iterable[,iterable2])">map(function, iterable[,iterable2])</a>
                        </li>
                    
                
            
            
            
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#reduce">reduce</a>
                        </li>
                    
                
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#filter">filter</a>
                        </li>
                    
                
            </ul>
</div>
  <div tabindex="-1" id="notebook" class="border-box-sizing">
    <div class="container" id="notebook-container">

<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[1]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="c1"># 打印所有单行变量</span>
<span class="kn">from</span> <span class="nn">IPython.core.interactiveshell</span> <span class="k">import</span> <span class="n">InteractiveShell</span>
<span class="n">InteractiveShell</span><span class="o">.</span><span class="n">ast_node_interactivity</span> <span class="o">=</span> <span class="s1">&#39;all&#39;</span>
</pre></div>

</div>
</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="&#20989;&#25968;">&#20989;&#25968;<a class="anchor-link" href="#&#20989;&#25968;">&#182;</a></h2>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[1]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="nb">str</span><span class="p">):</span>  <span class="c1"># 定义函数</span>
    <span class="nb">print</span><span class="p">(</span><span class="nb">str</span><span class="p">)</span>
    <span class="k">return</span>

<span class="n">foo</span><span class="p">(</span><span class="s2">&quot;python&quot;</span><span class="p">)</span>  <span class="c1"># 调用函数</span>
<span class="n">foo</span>            <span class="c1"># --&gt; 函数对象指向foo变量</span>
<span class="n">foo</span> <span class="o">=</span> <span class="mi">10</span>       <span class="c1"># 重定义</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt"></div>


<div class="output_subarea output_stream output_stdout output_text">
<pre>python
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="return&#35821;&#21477;">return&#35821;&#21477;<a class="anchor-link" href="#return&#35821;&#21477;">&#182;</a></h3><p>return语句用于<code>退出函数</code>，选择性地向调用方<code>返回</code>一个表达式。不带参数值的return语句返回None。</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[3]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">sum</span><span class="p">(</span> <span class="n">arg1</span><span class="p">,</span> <span class="n">arg2</span> <span class="p">):</span>
    <span class="n">total</span> <span class="o">=</span> <span class="n">arg1</span> <span class="o">+</span> <span class="n">arg2</span>
    <span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;函数内 : &quot;</span><span class="p">,</span> <span class="n">total</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">total</span>

<span class="n">total</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">(</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">20</span> <span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;返回值 : &quot;</span><span class="p">,</span> <span class="n">total</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt"></div>


<div class="output_subarea output_stream output_stdout output_text">
<pre>函数内 :  30
返回值 :  30
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="&#20989;&#25968;&#30340;&#23646;&#24615;">&#20989;&#25968;&#30340;&#23646;&#24615;<a class="anchor-link" href="#&#20989;&#25968;&#30340;&#23646;&#24615;">&#182;</a></h3><h4 id="__doc__"><code>__doc__</code><a class="anchor-link" href="#__doc__">&#182;</a></h4><p>前面用dir()查看属性和方法时，里面有个<code>__doc__</code>属性，便于阅读函数的使用方法和注释。</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[7]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">myfun</span><span class="p">():</span>
    <span class="s1">&#39;this is my function&#39;</span>  <span class="c1"># 本质就是字符串，可以用三引号</span>
    <span class="k">pass</span>

<span class="n">myfun</span><span class="o">.</span><span class="vm">__doc__</span>  <span class="c1"># --&gt;</span>
<span class="n">help</span><span class="p">(</span><span class="n">myfun</span><span class="p">)</span>    <span class="c1"># --&gt;</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[7]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>&#39;this is my function&#39;</pre>
</div>

</div>

<div class="output_area">

<div class="prompt"></div>


<div class="output_subarea output_stream output_stdout output_text">
<pre>Help on function myfun in module __main__:

myfun()
    this is my function

</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="&#22686;&#21152;&#20989;&#25968;&#23646;&#24615;">&#22686;&#21152;&#20989;&#25968;&#23646;&#24615;<a class="anchor-link" href="#&#22686;&#21152;&#20989;&#25968;&#23646;&#24615;">&#182;</a></h4>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[9]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">myfun</span><span class="o">.</span><span class="n">newnum</span> <span class="o">=</span> <span class="mi">10</span>       <span class="c1"># 增加一个newnum函数属性</span>

<span class="n">myfun</span><span class="o">.</span><span class="n">newnum</span>            <span class="c1"># --&gt;</span>
<span class="s1">&#39;newnum&#39;</span> <span class="ow">in</span> <span class="nb">dir</span><span class="p">(</span><span class="n">myfun</span><span class="p">)</span>  <span class="c1"># --&gt;</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[9]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>10</pre>
</div>

</div>

<div class="output_area">

<div class="prompt output_prompt">Out[9]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>True</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>上面属性用双下划线开始和结束，这类属性可以称之为<strong>特殊属性</strong>。</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[15]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">myfun</span><span class="o">.</span><span class="vm">__name__</span>     <span class="c1"># --&gt; 在创建函数时就已定义好</span>
<span class="n">myfun</span><span class="o">.</span><span class="n">__getname__</span>  <span class="c1"># &gt;_&lt; 没有定义的属性不能调用</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[15]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>&#39;myfun&#39;</pre>
</div>

</div>

<div class="output_area">

<div class="prompt"></div>


<div class="output_subarea output_text output_error">
<pre>
<span class="ansi-red-fg">---------------------------------------------------------------------------</span>
<span class="ansi-red-fg">AttributeError</span>                            Traceback (most recent call last)
<span class="ansi-green-fg">&lt;ipython-input-15-279966f84c2c&gt;</span> in <span class="ansi-cyan-fg">&lt;module&gt;</span><span class="ansi-blue-fg">()</span>
<span class="ansi-green-intense-fg ansi-bold">      1</span> myfun<span class="ansi-blue-fg">.</span>__name__     <span class="ansi-red-fg"># --&gt; 在创建函数时就已定义好</span>
<span class="ansi-green-fg">----&gt; 2</span><span class="ansi-red-fg"> </span>myfun<span class="ansi-blue-fg">.</span>__getname__  <span class="ansi-red-fg"># &gt;_&lt; 没有定义的属性不能调用</span>

<span class="ansi-red-fg">AttributeError</span>: &#39;function&#39; object has no attribute &#39;__getname__&#39;</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="&#21442;&#25968;">&#21442;&#25968;<a class="anchor-link" href="#&#21442;&#25968;">&#182;</a></h3><blockquote><p>在定义函数的时候，函数名后面的括号里如果有变量，它们通常被称为“形参”。<br>
调用函数的时候，给函数提供的值叫做“实参”，或者“参数”。<br>
可以将形参当作一个停车位，而将实参当作一辆汽车。 就像一个停车位可以在不同时间停放不同的汽车一样。</p>
</blockquote>
<p>以下是调用函数时可使用的正式参数类型：</p>
<ul>
<li>必需参数</li>
<li>关键字参数</li>
<li>默认参数</li>
<li>不定长参数</li>
</ul>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="&#24517;&#38656;&#21442;&#25968;">&#24517;&#38656;&#21442;&#25968;<a class="anchor-link" href="#&#24517;&#38656;&#21442;&#25968;">&#182;</a></h4><p>必需参数会以一一对应的顺序传入函数，调用时的数量必须和声明时的一样。</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[17]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="c1"># 定义函数</span>
<span class="k">def</span> <span class="nf">myfun</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">age</span><span class="p">):</span>
    <span class="s2">&quot;打印任何传入的字符串&quot;</span>    <span class="c1"># __doc__属性</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;名字: &quot;</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>  <span class="c1"># 结尾分号可用可不用</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;年龄: &quot;</span><span class="p">,</span> <span class="n">age</span><span class="p">)</span>

<span class="c1"># 调用myfun函数</span>
<span class="n">myfun</span><span class="p">(</span><span class="s2">&quot;wanli&quot;</span><span class="p">,</span> <span class="mi">26</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt"></div>


<div class="output_subarea output_stream output_stdout output_text">
<pre>名字:  wanli
年龄:  26
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[18]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">myfun</span><span class="p">(</span><span class="s1">&#39;wanli&#39;</span><span class="p">)</span>  <span class="c1"># &gt;_&lt; 缺少参数会报错</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt"></div>


<div class="output_subarea output_text output_error">
<pre>
<span class="ansi-red-fg">---------------------------------------------------------------------------</span>
<span class="ansi-red-fg">TypeError</span>                                 Traceback (most recent call last)
<span class="ansi-green-fg">&lt;ipython-input-18-9fb0ee3c9cbf&gt;</span> in <span class="ansi-cyan-fg">&lt;module&gt;</span><span class="ansi-blue-fg">()</span>
<span class="ansi-green-fg">----&gt; 1</span><span class="ansi-red-fg"> </span>myfun<span class="ansi-blue-fg">(</span><span class="ansi-blue-fg">&#39;wanli&#39;</span><span class="ansi-blue-fg">)</span>

<span class="ansi-red-fg">TypeError</span>: myfun() missing 1 required positional argument: &#39;age&#39;</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="&#20851;&#38190;&#23383;&#21442;&#25968;">&#20851;&#38190;&#23383;&#21442;&#25968;<a class="anchor-link" href="#&#20851;&#38190;&#23383;&#21442;&#25968;">&#182;</a></h4><p>使用关键字参数允许函数调用时参数的顺序与声明时不一致，因为 Python 解释器能够用参数名匹配参数值。</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[20]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">myfun</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">age</span><span class="p">):</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;名字: &quot;</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;年龄: &quot;</span><span class="p">,</span> <span class="n">age</span><span class="p">)</span>

<span class="n">myfun</span><span class="p">(</span><span class="n">age</span><span class="o">=</span><span class="mi">26</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s2">&quot;wanli&quot;</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt"></div>


<div class="output_subarea output_stream output_stdout output_text">
<pre>名字:  wanli
年龄:  26
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="&#40664;&#35748;&#21442;&#25968;">&#40664;&#35748;&#21442;&#25968;<a class="anchor-link" href="#&#40664;&#35748;&#21442;&#25968;">&#182;</a></h4><p>调用函数时，如果没有传递参数，则会使用默认参数</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[21]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">myfun</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">age</span><span class="o">=</span><span class="mi">26</span><span class="p">):</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;名字: &quot;</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;年龄: &quot;</span><span class="p">,</span> <span class="n">age</span><span class="p">)</span>

<span class="n">myfun</span><span class="p">(</span><span class="s2">&quot;wanli&quot;</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt"></div>


<div class="output_subarea output_stream output_stdout output_text">
<pre>名字:  wanli
年龄:  26
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="&#19981;&#23450;&#38271;&#21442;&#25968;">&#19981;&#23450;&#38271;&#21442;&#25968;<a class="anchor-link" href="#&#19981;&#23450;&#38271;&#21442;&#25968;">&#182;</a></h4><h5 id="*args">*args<a class="anchor-link" href="#*args">&#182;</a></h5>
<pre><code>def foo(*args):
    pass

</code></pre>
<p>用<code>*arg</code>形式收集参数会存放所有未命名的变量参数并返回一个元组。如果在函数调用时没有指定参数，它就是一个空元组。</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[25]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">):</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">args</span><span class="p">)</span>

<span class="n">foo</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="s1">&#39;python&#39;</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt"></div>


<div class="output_subarea output_stream output_stdout output_text">
<pre>(1, &#39;python&#39;, 3)
&lt;class &#39;tuple&#39;&gt;
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h5 id="**kargs">**kargs<a class="anchor-link" href="#**kargs">&#182;</a></h5>
<pre><code>def foo(**kargs):
    pass

</code></pre>
<p>用<code>**kargs</code>的形式收集参数，会得到dict类型的数据，但是，需要在传参数的时候说明“键”和“值”，因为在字典中是以键值对形式出现的。</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[26]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="o">**</span><span class="n">kargs</span><span class="p">):</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">kargs</span><span class="p">)</span>

<span class="n">foo</span><span class="p">(</span><span class="n">a</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span><span class="n">b</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span><span class="n">c</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt"></div>


<div class="output_subarea output_stream output_stdout output_text">
<pre>{&#39;b&#39;: 2, &#39;a&#39;: 1, &#39;c&#39;: 3}
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="&#22810;&#31181;&#31867;&#22411;&#21442;&#25968;">&#22810;&#31181;&#31867;&#22411;&#21442;&#25968;<a class="anchor-link" href="#&#22810;&#31181;&#31867;&#22411;&#21442;&#25968;">&#182;</a></h4>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[27]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">,</span><span class="n">z</span><span class="p">,</span><span class="o">*</span><span class="n">args</span><span class="p">,</span><span class="o">**</span><span class="n">kargs</span><span class="p">):</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">z</span><span class="p">)</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">args</span><span class="p">)</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">kargs</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[28]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">foo</span><span class="p">(</span><span class="s1">&#39;wanli&#39;</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="s2">&quot;python&quot;</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt"></div>


<div class="output_subarea output_stream output_stdout output_text">
<pre>wanli
2
python
()
{}
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[29]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">foo</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt"></div>


<div class="output_subarea output_stream output_stdout output_text">
<pre>1
2
3
(4, 5)
{}
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[30]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">foo</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="n">name</span><span class="o">=</span><span class="s2">&quot;wanli&quot;</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt"></div>


<div class="output_subarea output_stream output_stdout output_text">
<pre>1
2
3
(4, 5)
{&#39;name&#39;: &#39;wanli&#39;}
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="&#35299;&#21253;&#23454;&#21442;&#35843;&#29992;&#20989;&#25968;">&#35299;&#21253;&#23454;&#21442;&#35843;&#29992;&#20989;&#25968;<a class="anchor-link" href="#&#35299;&#21253;&#23454;&#21442;&#35843;&#29992;&#20989;&#25968;">&#182;</a></h4><p>前面说到传递任意数量的实参时会将它们打包进一个元组或字典，当然有打包也就有解包（unpacking）。通过单星号和双星号对List、Tuple和Dictionary进行解包：</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[37]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">fun</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">c</span><span class="p">):</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="o">+</span><span class="n">b</span><span class="o">+</span><span class="n">c</span><span class="p">)</span>

<span class="n">x</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span>
<span class="n">d</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;a&#39;</span><span class="p">:</span><span class="mi">4</span><span class="p">,</span> <span class="s1">&#39;b&#39;</span><span class="p">:</span><span class="mi">5</span><span class="p">,</span> <span class="s1">&#39;c&#39;</span><span class="p">:</span><span class="mi">6</span><span class="p">}</span>

<span class="n">fun</span><span class="p">(</span><span class="o">*</span><span class="n">x</span><span class="p">)</span>   <span class="c1"># --&gt; *解包list和tuple</span>
<span class="n">fun</span><span class="p">(</span><span class="o">**</span><span class="n">d</span><span class="p">)</span>  <span class="c1"># --&gt; **解包dict</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt"></div>


<div class="output_subarea output_stream output_stdout output_text">
<pre>6
15
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="&#36882;&#24402;">&#36882;&#24402;<a class="anchor-link" href="#&#36882;&#24402;">&#182;</a></h3><blockquote><p>递归（recursion），又译为递回，在数学与计算机科学中，是指在函数的定义中使用函数自身的方法。</p>
</blockquote>
<p>根据斐波那契数列的定义，可以直接写成这样的斐波那契数列递归函数。</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[38]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">fib</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    This is Fibonacci by Recursion.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">n</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
        <span class="k">return</span> <span class="mi">0</span>
    <span class="k">elif</span> <span class="n">n</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
        <span class="k">return</span> <span class="mi">1</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">fib</span><span class="p">(</span><span class="n">n</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="o">+</span> <span class="n">fib</span><span class="p">(</span><span class="n">n</span><span class="o">-</span><span class="mi">2</span><span class="p">)</span>

<span class="n">n</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">input</span><span class="p">(</span><span class="s1">&#39;输入正整数:&#39;</span><span class="p">))</span>
<span class="n">fib</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt"></div>


<div class="output_subarea output_stream output_stdout output_text">
<pre>输入正整数:4
</pre>
</div>
</div>

<div class="output_area">

<div class="prompt output_prompt">Out[38]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>3</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>解析：<br>
当n = 4时：</p>

<pre><code>fib(4) = fib(3)        +     fib(2)
             ↓                   ↓
      fib(2) + fib(1)     fib(1) + fib(0)
          ↓
   fib(1) + fib(0)</code></pre>
<p>相当于fib(4) = 3<em>fib(1) + 2</em>fib(0) = 3</p>
<blockquote><p>上面的代码每次递回下一级函数，都要判断一次判断一次if和elif<br>
用递归函数要小心，因为很容易陷入死循环</p>
</blockquote>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="&#20989;&#25968;&#24335;&#32534;&#31243;">&#20989;&#25968;&#24335;&#32534;&#31243;<a class="anchor-link" href="#&#20989;&#25968;&#24335;&#32534;&#31243;">&#182;</a></h2><p>Python是支持多种范型的语言，可以进行所谓函数式编程，其突出体现在有这么几个函数：filter、map、reduce、lambda、yield。</p>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="&#21311;&#21517;&#20989;&#25968;lambda">&#21311;&#21517;&#20989;&#25968;lambda<a class="anchor-link" href="#&#21311;&#21517;&#20989;&#25968;lambda">&#182;</a></h3><ul>
<li>在lambda后面直接跟变量</li>
<li>变量后面是冒号</li>
<li>冒号后面是表达式，表达式计算结果就是本函数的返回值</li>
</ul>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[4]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">func1</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span> <span class="o">*</span> <span class="n">x</span>      <span class="c1"># &lt;=&gt; def func1(x): return(x * x)</span>
<span class="n">func2</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span> <span class="o">%</span> <span class="mi">2</span>      <span class="c1"># &lt;=&gt; def func2(x): return(x % 2)</span>
<span class="n">func3</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">:</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>  <span class="c1"># &lt;=&gt; def func3(x, y): return (x, y)</span>

<span class="n">func1</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="n">func2</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
<span class="n">func3</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">6</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[4]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>9</pre>
</div>

</div>

<div class="output_area">

<div class="prompt output_prompt">Out[4]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>1</pre>
</div>

</div>

<div class="output_area">

<div class="prompt output_prompt">Out[4]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>(1, 6)</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="map(function,-iterable[,iterable2])">map(function, iterable[,iterable2])<a class="anchor-link" href="#map(function,-iterable[,iterable2])">&#182;</a></h3><ol>
<li>iterable的数量必须和function的参数一致</li>
<li>按顺序一一对应从可迭代对象传入参数</li>
<li>函数返回的值是一个以list的形式的map对象</li>
</ol>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[7]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">lst1</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">]</span>
<span class="n">lst2</span> <span class="o">=</span> <span class="p">[</span><span class="mi">6</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">0</span><span class="p">]</span>
<span class="n">lst3</span> <span class="o">=</span> <span class="p">[</span><span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">]</span>

<span class="n">x</span> <span class="o">=</span> <span class="nb">map</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">,</span><span class="n">z</span><span class="p">:</span> <span class="n">x</span><span class="o">+</span><span class="n">y</span><span class="o">+</span><span class="n">z</span><span class="p">,</span> <span class="n">lst1</span><span class="p">,</span> <span class="n">lst2</span><span class="p">,</span> <span class="n">lst3</span><span class="p">)</span>
<span class="n">x</span>        <span class="c1"># --&gt; map对象</span>
<span class="nb">list</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>  <span class="c1"># --&gt;</span>

<span class="n">y</span> <span class="o">=</span> <span class="p">[</span><span class="n">x</span><span class="o">+</span><span class="n">y</span><span class="o">+</span><span class="n">z</span> <span class="k">for</span> <span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">,</span><span class="n">z</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">lst1</span><span class="p">,</span> <span class="n">lst2</span><span class="p">,</span> <span class="n">lst3</span><span class="p">)]</span>
<span class="n">y</span>        <span class="c1"># --&gt;</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[7]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>&lt;map at 0x7f47cc520dd8&gt;</pre>
</div>

</div>

<div class="output_area">

<div class="prompt output_prompt">Out[7]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>[14, 17, 20, 15, 6]</pre>
</div>

</div>

<div class="output_area">

<div class="prompt output_prompt">Out[7]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>[14, 17, 20, 15, 6]</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="reduce">reduce<a class="anchor-link" href="#reduce">&#182;</a></h3>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[8]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">from</span> <span class="nn">functools</span> <span class="k">import</span> <span class="n">reduce</span>
<span class="n">reduce</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">:</span> <span class="n">x</span><span class="o">*</span><span class="n">y</span><span class="p">,</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">])</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[8]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>120</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="filter">filter<a class="anchor-link" href="#filter">&#182;</a></h3>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[10]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">numbers</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span>
<span class="n">f</span> <span class="o">=</span> <span class="nb">filter</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="o">&gt;</span><span class="mi">0</span><span class="p">,</span> <span class="n">numbers</span><span class="p">)</span>

<span class="n">f</span>        <span class="c1"># --&gt;</span>
<span class="nb">list</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>  <span class="c1"># --&gt;</span>
<span class="p">[</span><span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">numbers</span> <span class="k">if</span> <span class="n">x</span><span class="o">&gt;</span><span class="mi">0</span><span class="p">]</span>  <span class="c1"># --&gt;</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[10]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>&lt;filter at 0x7f47cc5c74a8&gt;</pre>
</div>

</div>

<div class="output_area">

<div class="prompt output_prompt">Out[10]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>[1, 2]</pre>
</div>

</div>

<div class="output_area">

<div class="prompt output_prompt">Out[10]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>[1, 2]</pre>
</div>

</div>

</div>
</div>

</div>
    </div>
  </div>
</body>

 


</html>
